package cn.edu.zucc.online_market.util;


import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DBPool{
	private static final String jdbcUrl="jdbc:mysql://localhost:3306/online_market";
	private static final String dbUser="root";
	private static final String dbPwd="123456";
	private static DBPool dbpool;
	private ComboPooledDataSource dataSource;
	static {
		dbpool = new DBPool();
	}
	
	public DBPool(){
		try {
			dataSource = new ComboPooledDataSource();
			dataSource.setUser(dbUser);
			dataSource.setPassword(dbPwd);
			dataSource.setJdbcUrl(jdbcUrl);
			dataSource.setDriverClass("com.mysql.jdbc.Driver"); //����Դ��������
			dataSource.setInitialPoolSize(2);
			dataSource.setMinPoolSize(1);
			dataSource.setMaxPoolSize(10);
			dataSource.setMaxStatements(50); //���ӳػ����Statement�����
			dataSource.setMaxIdleTime(60); //���ӵ�������ʱ��	
		}catch(PropertyVetoException e) {
			throw new RuntimeException(e);
		}
		
		
	}
	static{
		try {
			Class.forName("com.mysql.jdbc.Driver");
		}catch(ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	public static final DBPool getInstance() {
		return dbpool;
	}
	public final Connection getConnection() {
		try {
			return dataSource.getConnection();
		}catch(SQLException e) {
			throw new RuntimeException("�޷�������Դ�������",e);
		}
		
	}
	
	
}